/*
 * TagManager.java
 *
 * Created on March 27, 2007, 7:09 PM
 *
 * To change this template, choose Tools | Template TagManager
 * and open the template in the editor.
 */

package Activity;

import Comm.Util.Logger;
import java.util.Enumeration;
import java.util.Vector;

/**
 *
 * @author dmgreen
 */
public class TagManager {
    
    //private Hashtable myTags;
    private Vector myTags;
    
    /**
     * Creates a new instance of TagManager
     */
    public TagManager() {
    
        //maps activity name to activity uid
        //myTags = new Hashtable();
    
        myTags = new Vector();
    }

    public int getIdForTag(int activity_index)  {
        int id;
        try {
            id = ((TagData) myTags.elementAt(activity_index)).getID();
        } catch (ArrayIndexOutOfBoundsException aoobe) {
            id = -1;
        }
                
        return id;
    }
    
    
    public String getNameForTag(int activity_index)  {
        String name;
        try {
            name = ((TagData) myTags.elementAt(activity_index)).getName();
        } catch (ArrayIndexOutOfBoundsException aoobe) {
            name = "";
        }
                
        return name;

    }
    
    public void synchTags(String tag_data) {
        
        Logger.getLog().addLogMessage("Synch tags called", Logger.MSG_DEBUG);
        
        if (tag_data == "") {
            myTags.removeAllElements();
            return;
        }
        
        // make a copy of the string
        String tags_str = new String(tag_data);
        
        Vector new_tagdata_objs = new Vector();
        
        boolean got_error = false;
        while (tags_str.indexOf(";") >= 0) {
            
            try {
                String indiv_tag = tags_str.substring(0,tags_str.indexOf(";"));
            
                TagData td = parseIndivTagData(indiv_tag);                         
                if (td != null) {
                    new_tagdata_objs.addElement(td);
                } else {
                    got_error = true;
                }

                tags_str = tags_str.substring(tags_str.indexOf(";")+1);
            } catch (Exception e) {
                Logger.getLog().addLogMessage("Error synching tags: " + e.getMessage(), Logger.MSG_ERROR);
                return;
            }
        }
        
        // you still have one more left out here ...
        try {
            TagData td = parseIndivTagData(tags_str);
            new_tagdata_objs.addElement(td);
        } catch (Exception e) {
            Logger.getLog().addLogMessage("Error synching tags: " + e.getMessage(), Logger.MSG_ERROR);
            return;
        }

        myTags.removeAllElements();
        Enumeration en = new_tagdata_objs.elements();
        while (en.hasMoreElements()) {
            myTags.addElement(en.nextElement());
        }
           
        Logger.getLog().addLogMessage("Successfully synched", Logger.MSG_DEBUG);
    }
    
    private TagData parseIndivTagData(String a_tag) throws Exception {
        TagData a_tagdata = new TagData();
        
        String id_string = a_tag.substring(0,a_tag.indexOf(","));
        a_tagdata.setID( Integer.parseInt(id_string));
            
        String name_string = a_tag.substring(a_tag.indexOf(",")+1, a_tag.lastIndexOf(','));
        a_tagdata.setName(name_string);
            
        String uses_string = a_tag.substring(a_tag.lastIndexOf(',')+1);
        a_tagdata.setUses(Integer.parseInt(uses_string));
        
        return a_tagdata;
    }
    
    public String[] getTagNames() {
        // returns an array of strings representing all activities
        
        String[] tag_array = new String[myTags.size()];
       
        for (int i=0; i<myTags.size(); i++) {
            TagData td = (TagData) myTags.elementAt(i);
            tag_array[i] = td.getName() + " (" + td.getUses() + ")";
        }
        
        return tag_array;        
    }
    
    public void addNewTags(String new_tags) {
        
        Vector new_tagdata_objs = new Vector();
        boolean got_error = false;
        
        while (new_tags.indexOf(";") >= 0) {
            
            try {
                String indiv_tag = new_tags.substring(0,new_tags.indexOf(";"));
            
                TagData td = new TagData();
                td.setName(indiv_tag);
                td.setUses(1);
                td.setID(-1);
                
                new_tagdata_objs.addElement(td);

                new_tags = new_tags.substring(new_tags.indexOf(";")+1);
            } catch (Exception e) {
                Logger.getLog().addLogMessage("Error parsing new tags: " + e.getMessage(), Logger.MSG_ERROR);
                got_error = true;
                break;
            }
        } // END WHILE
        
        if (!new_tags.equals("")) {
            new_tagdata_objs.addElement(new TagData(-1, new_tags, 1));
        }
        
        
        if (!got_error) {
            Enumeration en = new_tagdata_objs.elements();
            
            while (en.hasMoreElements()) {
                TagData obj = (TagData) en.nextElement();
                myTags.addElement(obj);
            }
        } // END IF
    }
}
